home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / what's new? / sample code / graphics 3d / setupgl / setupgl main.c < prev    next >
Encoding:
Text File  |  1999-12-18  |  17.9 KB  |  639 lines

  1. /*
  2.     File:        SetupGL Main.c
  3.  
  4.     Contains:    An example of the use of the SeupGL utility code.  This sample builds a context and lets the user control
  5.                 the specifications on the fly, rebuilding the context when the request changes.  Controls are:
  6.                 Left/right arrows: smaller or larger resolution context
  7.                 Up/down arrows: 32/16 bit pixel depth toggle
  8.                 left/right bracket: chnage requested screen down or up one
  9.                 w: go to windowed mode
  10.                 f: go to fullscreen mode            
  11.  
  12.     Written by:    Geoff Stahl
  13.  
  14.     Copyright:    1999 Apple Computer, Inc., All Rights Reserved
  15.  
  16.     Change History (most recent first):
  17.  
  18.  
  19.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  20.                 ("Apple") in consideration of your agreement to the following terms, and your
  21.                 use, installation, modification or redistribution of this Apple software
  22.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  23.                 please do not use, install, modify or redistribute this Apple software.
  24.  
  25.                 In consideration of your agreement to abide by the following terms, and subject
  26.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  27.                 copyrights in this original Apple software (the "Apple Software"), to use,
  28.                 reproduce, modify and redistribute the Apple Software, with or without
  29.                 modifications, in source and/or binary forms; provided that if you redistribute
  30.                 the Apple Software in its entirety and without modifications, you must retain
  31.                 this notice and the following text and disclaimers in all such redistributions of
  32.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  33.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  34.                 Apple Software without specific prior written permission from Apple.  Except as
  35.                 expressly stated in this notice, no other rights or licenses, express or implied,
  36.                 are granted by Apple herein, including but not limited to any patent rights that
  37.                 may be infringed by your derivative works or by other works in which the Apple
  38.                 Software may be incorporated.
  39.  
  40.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  41.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  42.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  44.                 COMBINATION WITH YOUR PRODUCTS.
  45.  
  46.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  47.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  48.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  49.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  50.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  51.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  52.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53.     
  54.          <4>    12/18/99    ggs     Fix headers
  55. */
  56.  
  57.  
  58. // system includes ----------------------------------------------------------
  59.  
  60. #include <Devices.h>
  61. #include <Dialogs.h>
  62. #include <DriverServices.h>
  63. #include <Events.h>
  64. #include <LowMem.h>
  65. #include <TextEdit.h>
  66. #include <ToolUtils.h>
  67. #include <Windows.h>
  68.  
  69. #include <math.h>
  70. #include <stdio.h>
  71. #include <string.h>
  72.  
  73. #include <gl.h>
  74.  
  75. // project includes ---------------------------------------------------------
  76.  
  77. #include "SetupGL.h"
  78.  
  79. // prototypes ---------------------------------------------------------------
  80.  
  81. void InitToolbox(void);
  82. Boolean SetUp (void);
  83. void DoMenu (SInt32 menuResult);
  84. void DoKey (SInt8 theKey, SInt8 theCode);
  85. void DoUpdate (void);
  86. void DoEvent (void);
  87. void CleanUp (void);
  88.  
  89. void drawGL(AGLContext aglContext, pstructGLInfo pglInfo);
  90.  
  91. void DrawFrameRate (GLuint fontList);
  92. void DrawContextInfo (GLuint fontList);
  93.  
  94. void DrawPSringGL (Str255 pstrOut, GLuint fontList);
  95. void DrawCSringGL (char * cstrOut, GLuint fontList);
  96. GLuint BuildFontGL (AGLContext ctx, GLint fontID, Style face, GLint size);
  97. void DeleteFontGL (GLuint fontList);
  98.  
  99.  
  100. // statics/globals (internal only) ------------------------------------------
  101.  
  102. // Menu defs
  103. enum 
  104. {
  105.     kNumRes = 10,
  106.     kNumFreqs = 11,
  107.  
  108.     kMenuApple = 128,
  109.     kMenuFile = 129,
  110.     
  111.     kAppleAbout = 1,
  112.     kFileQuit = 1
  113. };
  114.  
  115. enum 
  116. {
  117.     kForegroundSleep = 0,
  118.     kBackgroundSleep = 32000
  119. };
  120.  
  121. const RGBColor    rgbWhite    = { 0xFFFF, 0xFFFF, 0xFFFF };
  122.  
  123. SInt32 gSleepTime = kForegroundSleep;
  124. Boolean gDone = false, gfFrontProcess = true;
  125.  
  126. Point gapntResolution [kNumRes] = {{300, 400}, {384, 512}, {480, 640}, {600, 800}, {624, 832}, {768, 1024}, {870, 1152}, {1024, 1280}, {1600, 1200}, {1920, 1440}};
  127. short gResCurrent = 2;
  128. long gaFreq [kNumFreqs] = {56, 60, 65, 67, 72, 75, 76, 85, 90, 100, 120};
  129. short gFreqCurrent = 7;
  130. short gNumDevice = 0;
  131. char gstrMode [256] = "";
  132.  
  133. structGLInfo glInfo;
  134. AGLDrawable aglDraw = 0;
  135. AGLContext aglContext = 0;
  136. DSpContextReference dspContext = 0;
  137. GLuint gFontList = 0;
  138.  
  139. WindowPtr gpWindowError = NULL;
  140.  
  141.  
  142. // functions (internal/private) ---------------------------------------------
  143.  
  144. void InitToolbox(void)
  145. {
  146.     MenuHandle menu;
  147.  
  148.     MaxApplZone ();
  149.  
  150.     InitGraf((Ptr) &qd.thePort);
  151.     InitFonts();
  152.     InitWindows();
  153.     InitMenus();
  154.     TEInit();
  155.     InitDialogs(nil);
  156.     InitCursor();
  157.     
  158.     qd.randSeed =  TickCount();
  159.  
  160.     // Init Menus
  161.     menu = NewMenu (kMenuApple, "\p\024");            // new  apple menu
  162.     InsertMenu (menu, 0);                            // add menu to end
  163.     AppendResMenu(menu, 'DRVR');
  164.     
  165.     menu = NewMenu (kMenuFile, "\pFile");            // new menu
  166.     InsertMenu (menu, 0);                            // add menu to end
  167.     AppendMenu (menu, "\pQuit/Q"); // add items
  168.     
  169.     DrawMenuBar();
  170.  
  171. // --------------------------------------------------------------------------
  172.  
  173. Boolean SetUp (void)
  174. {
  175.     short i;
  176.     short fNum;
  177.  
  178.     InitToolbox ();
  179.     
  180.     // we want a context and a drawable    
  181.     gNumDevice = 0;
  182.     memset(&glInfo, 0, sizeof glInfo);
  183.     glInfo.width = gapntResolution [gResCurrent].h; // width of drawable (screen width in full screen mode)
  184.     glInfo.height = gapntResolution [gResCurrent].v; // height of drawable (screen height in full screen mode)
  185.     glInfo.fSizeMust = false;            // dspContext must be requested height (ignored in window mode)
  186.                                         // Note: This basically determines whether or not displays will step down to allocate or fail
  187.     glInfo.pixelDepth = 16;                // requested pixel depth
  188.     glInfo.fDepthMust = false;            // pixel depth must be set (if false then curretn depth will be used if able)
  189.     glInfo.fFullscreen = true;            // use DSp to get fullscreen?
  190.     glInfo.fAcceleratedMust = true;     // must renderer be accelerated?
  191.     glInfo.VRAM = 0 * 1048576;            // minimum VRAM (if not zero this is always required)
  192.     glInfo.textureRAM = 0 * 1048576;    // minimum texture RAM (if not zero this is always required)
  193.     glInfo.freq = gaFreq [gFreqCurrent]; // desired vertical refresh frquency in Hz (0 = any)
  194.     glInfo.fmt = 0;                        // output pixel format
  195.     
  196.     i = 0;
  197.     glInfo.aglAttributes [i++] = AGL_RGBA;
  198.     glInfo.aglAttributes [i++] = AGL_DOUBLEBUFFER;
  199.     glInfo.aglAttributes [i++] = AGL_NO_RECOVERY;
  200.     glInfo.aglAttributes [i++] = AGL_ACCELERATED;
  201.     glInfo.aglAttributes [i++] = AGL_RED_SIZE;
  202.     glInfo.aglAttributes [i++] = 8;
  203.     glInfo.aglAttributes [i++] = AGL_GREEN_SIZE;
  204.     glInfo.aglAttributes [i++] = 8;
  205.     glInfo.aglAttributes [i++] = AGL_BLUE_SIZE;
  206.     glInfo.aglAttributes [i++] = 8;
  207.     glInfo.aglAttributes [i++] = AGL_ALPHA_SIZE;
  208.     glInfo.aglAttributes [i++] = 0;
  209.     glInfo.aglAttributes [i++] = AGL_DEPTH_SIZE;
  210.     glInfo.aglAttributes [i++] = 16;
  211.     glInfo.aglAttributes [i++] = AGL_ALL_RENDERERS;
  212.     glInfo.aglAttributes [i++] = AGL_NONE;
  213.     
  214.     BuildGL (&aglDraw, &aglContext, &dspContext, &gNumDevice, &glInfo);
  215.     if (!aglDraw || !aglContext)
  216.     {
  217.         Rect rectWin = {100, 100, 200, 400};
  218.         DestroyGL (&aglDraw, &aglContext, &dspContext, &glInfo);
  219.         gpWindowError = (WindowPtr) NewCWindow (NULL, &rectWin, "\p", 0, kWindowFloatProc, (WindowPtr)-1, 0, 0);
  220.         ShowWindow ((GrafPtr)gpWindowError);
  221.     }
  222.         
  223.     GetFNum("\pMonaco", &fNum);                                    // build font
  224.     gFontList = BuildFontGL (aglContext, fNum, normal, 9);
  225.     // set mode string
  226.     sprintf (gstrMode, "%d: %d x %d x %d (%d Hz)", gNumDevice, glInfo.width, glInfo.height, glInfo.pixelDepth, glInfo.freq);
  227.  
  228.     return true;
  229. }
  230.  
  231. // --------------------------------------------------------------------------
  232.  
  233. void DoMenu (SInt32 menuResult)
  234. {
  235.     SInt16 theMenu;
  236.     SInt16 theItem;
  237.     Str255 daName;
  238.     MenuRef theMenuHandle;
  239.         
  240.     theMenu = HiWord(menuResult);
  241.     theItem = LoWord(menuResult);
  242.     theMenuHandle = GetMenuHandle(theMenu);
  243.  
  244.     switch (theMenu)
  245.     {
  246.         case kMenuApple:
  247.             switch (theItem)
  248.             {
  249.                 case kAppleAbout:
  250.                     break;
  251.                 default:
  252.                     OpenDeskAcc(daName);
  253.                     break;
  254.             }
  255.             break;
  256.         case kMenuFile:
  257.             switch (theItem)
  258.             {
  259.                 case kFileQuit:
  260.                     gDone = true;
  261.                     break;
  262.             }
  263.             break;
  264.     }
  265.     HiliteMenu(0);
  266.     DrawMenuBar();
  267. }
  268.  
  269. // --------------------------------------------------------------------------
  270.  
  271. void DoKey (SInt8 theKey, SInt8 theCode)
  272. {
  273.     #pragma unused (theCode)
  274.     // up and down arrows change bit depth (stop at top and bottom)
  275.     // right and left arrows change size (stop at top and bottom)
  276.     // +(=) and - change frequency request
  277.     // [ and ] cnage requested display device
  278.     
  279.     short fNum;
  280.     
  281.     // resolution
  282.     if (theKey == '\35')
  283.     {
  284.         
  285.         if (++gResCurrent == kNumRes)
  286.             gResCurrent = 0;
  287.         glInfo.width = gapntResolution [gResCurrent].h;
  288.         glInfo.height = gapntResolution [gResCurrent].v;
  289.     }
  290.     else if (theKey == '\34')
  291.     {
  292.         if (--gResCurrent < 0)
  293.             gResCurrent = (short) (kNumRes - 1);
  294.         glInfo.width = gapntResolution [gResCurrent].h;
  295.         glInfo.height = gapntResolution [gResCurrent].v;
  296.     }
  297.     
  298.     // pixel depth
  299.     if (theKey == '\36')
  300.     {
  301.         glInfo.pixelDepth = 32;
  302.     }
  303.     else if (theKey == '\37')
  304.     {
  305.         glInfo.pixelDepth = 16;
  306.     }
  307.     
  308.     // frequency
  309.     if (theKey == '=')
  310.     {
  311.         
  312.         if (++gFreqCurrent == kNumFreqs)
  313.             gFreqCurrent = 0;
  314.         glInfo.freq = gaFreq [gFreqCurrent];
  315.     }
  316.     else if (theKey == '-')
  317.     {
  318.         if (--gFreqCurrent < 0)
  319.             gFreqCurrent = (short) (kNumFreqs - 1);
  320.         glInfo.freq = gaFreq [gFreqCurrent];
  321.     }
  322.     
  323.     // device 
  324.     if (theKey == ']')
  325.         gNumDevice++;
  326.     else if (theKey == '[')
  327.         gNumDevice--;
  328.     
  329.     // device 
  330.     if (theKey == 'w')
  331.         glInfo.fFullscreen = false;
  332.     else if (theKey == 'f')
  333.         glInfo.fFullscreen = true;
  334.     
  335.     glInfo.fSizeMust = false;            // dspContext must be requested height (ignored in window mode)
  336.     glInfo.fDepthMust = false;            // pixel depth must be set (if false then curretn depth will be used if able)
  337.     glInfo.fAcceleratedMust = true;     // must renderer be accelerated?
  338.     glInfo.VRAM = 0 * 1048576;            // minimum VRAM (if not zero this is always required)
  339.     glInfo.textureRAM = 0 * 1048576;    // minimum texture RAM (if not zero this is always required)
  340.  
  341.     // rebuild GL
  342.     if (gpWindowError)
  343.     {
  344.         DisposeWindow ((WindowPtr)gpWindowError);
  345.         gpWindowError = NULL;
  346.     }
  347.     DeleteFontGL (gFontList);
  348.     DestroyGL (&aglDraw, &aglContext, &dspContext, &glInfo);
  349.     BuildGL (&aglDraw, &aglContext, &dspContext, &gNumDevice, &glInfo);
  350.     if (!aglDraw || !aglContext)
  351.     {
  352.         Rect rectWin = {100, 100, 150, 200};
  353.         DestroyGL (&aglDraw, &aglContext, &dspContext, &glInfo);        
  354.         gpWindowError = (WindowPtr) NewCWindow (NULL, &rectWin, "\p", 0, kWindowFloatProc, (WindowPtr)-1, 0, 0);
  355.         ShowWindow ((GrafPtr)gpWindowError);
  356.     }
  357.             
  358.     GetFNum("\pMonaco", &fNum);                                    // build font
  359.     gFontList = BuildFontGL (aglContext, fNum, normal, 9);
  360.     // set mode string
  361.     sprintf (gstrMode, "%d: %d x %d x %d (%d Hz)", gNumDevice, glInfo.width, glInfo.height, glInfo.pixelDepth, glInfo.freq);
  362.     DoUpdate ();
  363. }
  364.  
  365. // --------------------------------------------------------------------------
  366.  
  367. void DoUpdate (void)
  368. {
  369.     if (aglDraw && aglContext)
  370.         drawGL (aglContext, &glInfo);
  371.     else
  372.     {
  373.         GrafPtr gpSave;
  374.         GetPort (&gpSave);
  375.         SetPort (gpWindowError);
  376.         MoveTo (10, 20);
  377.         DrawString ("\pBuildGL failed.");
  378.         SetPort (gpSave);
  379.     }
  380. }
  381.  
  382. // --------------------------------------------------------------------------
  383.  
  384. void DoEvent (void)
  385. {
  386.     EventRecord theEvent;
  387.     Rect rectGrow;
  388.     SInt32 menuResult;
  389.     WindowRef whichWindow;
  390.     long grow;
  391.     SInt16 whatPart;
  392.     SInt8 theKey;
  393.     SInt8 theCode;
  394.     Boolean fProcessed = false;
  395.     
  396.     if (WaitNextEvent(everyEvent, &theEvent, gSleepTime, NULL))
  397.     {
  398.         if (dspContext)
  399.             DSpProcessEvent (&theEvent, &fProcessed);
  400.         if (!fProcessed)
  401.         {
  402.             switch (theEvent.what)
  403.             {
  404.                 case mouseDown:
  405.                     whatPart = FindWindow(theEvent.where, &whichWindow);
  406.                     switch (whatPart)
  407.                     {
  408.                         case inGoAway:
  409.                             break;
  410.                         case inMenuBar:
  411.                             DrawMenuBar();
  412.                             menuResult = MenuSelect(theEvent.where);
  413.                             if (HiWord(menuResult) != 0)
  414.                                 DoMenu(menuResult);
  415.                             break;
  416.                         case inDrag:
  417.                             DragWindow (whichWindow, theEvent.where, &(**LMGetGrayRgn()).rgnBBox);
  418.                             break;
  419.                         case inGrow:
  420.                             SetRect (&rectGrow, 100, 100, 20000, 20000);
  421.                             grow = GrowWindow (whichWindow, theEvent.where, &rectGrow);
  422.                             if (grow)
  423.                             {
  424.                                 SizeWindow (whichWindow, grow & 0x0000FFFF, grow >> 16, true);
  425.                                 // do content stuff here
  426.                                 SetPort (whichWindow);
  427.                                 InvalRect (&whichWindow->portRect);                // redraw all
  428.                             }
  429.                             break;
  430.                         case inSysWindow:
  431.                             SystemClick(&theEvent, whichWindow);
  432.                             break;
  433.                     }
  434.                     break;
  435.                 case keyDown:
  436.                 case autoKey:
  437.                     theKey = theEvent.message & charCodeMask;
  438.                     theCode = (theEvent.message & keyCodeMask) >> 8;
  439.                     if ((theEvent.modifiers & cmdKey) != 0)
  440.                     {
  441.                         menuResult = MenuKey(theKey);
  442.                         if (HiWord(menuResult) != 0)
  443.                             DoMenu (menuResult);
  444.                     }
  445.                     else
  446.                         DoKey (theKey, theCode);
  447.                     break;
  448.                 case updateEvt:
  449.                     BeginUpdate((WindowRef) theEvent.message);
  450.                     DoUpdate();
  451.                     EndUpdate((WindowRef) theEvent.message);
  452.                     break;
  453.                 case diskEvt:
  454.                     break;
  455.                 case osEvt:
  456.                     if (theEvent.message & 0x01000000)        //    Suspend/resume event
  457.                     {
  458.                         if (theEvent.message & 0x00000001)    //    Resume
  459.                         {
  460.                             gSleepTime = kForegroundSleep;                            
  461.                             DoUpdate ();
  462.                             aglSetDrawable(aglContext, aglDraw);
  463.                             SelectWindow ((GrafPtr) aglDraw);
  464.                             gfFrontProcess = true;
  465.                         }
  466.                         else
  467.                             gSleepTime = kBackgroundSleep;    //    Suspend
  468.                             aglSetDrawable(aglContext, NULL);
  469.                             HideWindow((GrafPtr) aglDraw);
  470.                             gfFrontProcess = false;
  471.                     }
  472.                     break;
  473.  
  474.                 case kHighLevelEvent:
  475.                     AEProcessAppleEvent(&theEvent);
  476.                     break;
  477.             }
  478.         }
  479.     }
  480.     else
  481.     {
  482.         DoUpdate ();
  483.     }
  484. }
  485.  
  486. // --------------------------------------------------------------------------
  487.  
  488. void CleanUp (void)
  489. {
  490.     MenuHandle hMenu;
  491.  
  492.     glFinish ();                                                // flush and sync
  493.     DeleteFontGL (gFontList);
  494.     DestroyGL (&aglDraw, &aglContext, &dspContext, &glInfo);
  495.  
  496.     if (gpWindowError)
  497.     {
  498.         DisposeWindow ((WindowPtr)gpWindowError);
  499.         gpWindowError = NULL;
  500.     }
  501.  
  502.     hMenu = GetMenuHandle (kMenuFile);
  503.     DeleteMenu (kMenuFile);
  504.     DisposeMenu (hMenu);
  505.  
  506.     hMenu = GetMenuHandle (kMenuApple);
  507.     DeleteMenu (kMenuApple);
  508.     DisposeMenu (hMenu);
  509. }
  510.  
  511. // --------------------------------------------------------------------------
  512.  
  513. int main (void)
  514. {
  515.     if (SetUp ())    
  516.         while (!gDone) 
  517.             DoEvent ();
  518.     CleanUp ();
  519.     return 0;
  520. }
  521.  
  522.  
  523. #pragma mark -
  524. //-----------------------------------------------------------------------------------------------------------------------
  525.  
  526. // OpenGL Drawing
  527.  
  528. void drawGL(AGLContext aglContext, pstructGLInfo pglInfo)
  529. {
  530.     static float f, s, c;
  531.     GLboolean fState = GL_FALSE;
  532.  
  533.     f += 0.01;
  534.     s = sin(f);
  535.     c = cos(f);
  536.  
  537.     glClearColor(0.15f, 0.15f, 0.15f, 1.0f);                    // Clear color buffer to dark grey
  538.     glClear(GL_COLOR_BUFFER_BIT);
  539.     
  540.     glBegin(GL_POLYGON);                                        // Draw a smooth shaded polygon
  541.     glColor3d(1.0, 0.0, 0.0);
  542.     glVertex3d(s, c, 0.0);
  543.     glColor3d(0.0, 1.0, 0.0);
  544.     glVertex3d(-c, s, 0.0);
  545.     glColor3d(0.0, 0.0, 1.0);
  546.     glVertex3d(-s, -c, 0.0);
  547.     glColor3d(0.7, 0.7, 0.7);
  548.     glVertex3d(c, -s, 0.0);
  549.     glEnd();
  550.  
  551.     // Draw frame rate (set color and position first)
  552.     glColor3d(1.0, 1.0, 1.0);
  553.     glRasterPos3d (-120.0 / (float) pglInfo->width, (pglInfo->height - 40.0) / (float) pglInfo->height, 0.0); 
  554.     DrawFrameRate (gFontList);
  555.     glRasterPos3d (-160.0 / (float) pglInfo->width, (pglInfo->height - 65.0) / (float) pglInfo->height, 0.0); 
  556.     DrawContextInfo (gFontList);
  557.  
  558.     aglSwapBuffers(aglContext);                                        // send swap command
  559. }
  560.  
  561. //-----------------------------------------------------------------------------------------------------------------------
  562.  
  563. // Draw frame rate in curent color at current raster positon with provided font display list
  564.  
  565. void DrawFrameRate (GLuint fontList)
  566. {    
  567.     static char aChar[256] = "";
  568.     static AbsoluteTime time = {0,0};
  569.     static long frames = 0;
  570.  
  571.     AbsoluteTime currTime = UpTime ();
  572.     float deltaTime = (float) AbsoluteDeltaToDuration (currTime, time);
  573.     
  574.     frames++;
  575.  
  576.     if (0 > deltaTime)    // if negative microseconds
  577.         deltaTime /= -1000000.0;
  578.     else                // else milliseconds
  579.         deltaTime /= 1000.0;
  580.     if (0.5 <= deltaTime)    // has update interval passed
  581.     {
  582.         sprintf (aChar, "Swaps/Sec: %0.1f", frames / deltaTime);
  583.         time = currTime;    // reset for next time interval
  584.         frames = 0;
  585.     }
  586.     
  587.     DrawCSringGL (aChar, fontList);
  588. }
  589.  
  590. //-----------------------------------------------------------------------------------------------------------------------
  591.  
  592. // Draw height,width, bit pixel and frequency for context
  593.  
  594. void DrawContextInfo (GLuint fontList)
  595. {    
  596.     DrawCSringGL (gstrMode, fontList);
  597. }
  598.  
  599. #pragma mark -
  600. //-----------------------------------------------------------------------------------------------------------------------
  601.  
  602. void DrawPSringGL (Str255 pstrOut, GLuint fontList)
  603. {
  604.     GLint i;
  605.     for (i = 1; i <= pstrOut[0]; i++)
  606.         glCallList (fontList + pstrOut[i]);
  607. }
  608.  
  609. //-----------------------------------------------------------------------------------------------------------------------
  610.  
  611. void DrawCSringGL (char * cstrOut, GLuint fontList)
  612. {
  613.     GLint i = 0;
  614.     while (cstrOut [i])
  615.         glCallList (fontList + cstrOut[i++]);
  616. }
  617.  
  618. //-----------------------------------------------------------------------------------------------------------------------
  619.  
  620. GLuint BuildFontGL (AGLContext ctx, GLint fontID, Style face, GLint size)
  621. {
  622.     GLuint listBase = glGenLists (256);
  623.     if (aglUseFont (ctx, fontID , face, size, 0, 256, (long) listBase))
  624.         return listBase;
  625.     else
  626.     {
  627.         glDeleteLists (listBase, 256);
  628.         return 0;
  629.     }
  630. }
  631.  
  632. //-----------------------------------------------------------------------------------------------------------------------
  633.  
  634. void DeleteFontGL (GLuint fontList)
  635. {
  636.     if (fontList)
  637.         glDeleteLists (fontList, 256);
  638. }